# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from zab.models import *

from django.http import HttpResponse

from django.db import models

from django.shortcuts import render

# Register your models here.

def readAttr( object_list ):
    if ( len( object_list ) < 1 ):
        return []
    table = []
    for field in object_list[0]._meta.fields:
        if( isinstance(field, models.CharField)):
            row = []
            row.append(field.name)
            for _object in object_list:
                row.append( getattr( _object, field.name ) )
            table.append(row)
        if ( isinstance(field, models.ForeignKey)):
            row = [field.name]
            table.append(row)
            new_object_list = []
            for _object in object_list:
                new_object_list.append( getattr(_object, field.name) )
            table.extend( readAttr( new_object_list ) )
    return table


def compare(modeladmin, request, queryset):
    table = []
    var_list = [ var for var in queryset ]
    table.extend(readAttr(var_list ))
    print(table)
    return render(request, 'zab/test.html', {'table':table})
compare.short_description = "compare"

class ProjectAdmin(admin.ModelAdmin):
    list_display = ['project_text']
    ordering = ['project_text']
    actions = [compare]

admin.site.register(Project, ProjectAdmin)
admin.site.register(Handset_wb)
admin.site.register(Handhold_wb)
